import request from '@/utils/request'
import type { Result, PageParams, PageResult, LoginParams, LoginResult, UserInfo, CreateUserParams, UpdateUserParams } from '@/types/api'

export { UserInfo }

export const userApi = {
  // 用户登录
  async login(data: LoginParams): Promise<Result<LoginResult>> {
    return (await request.post<Result<LoginResult>>('/admin/users/login', data)).data
  },

  // 用户登出
  async logout(): Promise<Result<string>> {
    return (await request.post<Result<string>>('/admin/users/logout')).data
  },

  // 分页查询用户
  async getUserList(params: PageParams & { userType?: string }): Promise<Result<PageResult<UserInfo>>> {
    return (await request.get<Result<PageResult<UserInfo>>>('/admin/users/page', { params })).data
  },

  // 创建用户
  async createUser(data: CreateUserParams): Promise<Result<string>> {
    return (await request.post<Result<string>>('/admin/users', data)).data
  },

  // 更新用户
  async updateUser(data: UpdateUserParams): Promise<Result<string>> {
    return (await request.put<Result<string>>('/admin/users', data)).data
  },

  // 删除用户
  async deleteUser(userId: number): Promise<Result<string>> {
    return (await request.delete<Result<string>>(`/admin/users/${userId}`)).data
  },

  // 获取用户详情
  async getUserDetail(userId: number): Promise<Result<UserInfo>> {
    return (await request.get<Result<UserInfo>>(`/admin/users/${userId}`)).data
  },

  // 用户注册
  async register(data: CreateUserParams): Promise<Result<string>> {
    return (await request.post<Result<string>>('/admin/users/register', data)).data
  }
}

// 导出单独的函数，用于兼容性
export const { getUserList, createUser, updateUser, deleteUser } = userApi 